package top.infopub.mgr.ic.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.DesignExamAttDao;
import top.infopub.mgr.ic.dao.DesignExamDao;
import top.infopub.mgr.ic.domain.DesignExamDomain;
import top.infopub.model.ic.DesignExam;
import top.infopub.model.ic.DesignExamAtt;
import top.infopub.mgr.ic.service.DesignExamService;
import top.infopub.mgr.ic.vo.DesignExamVo;

@Service
public class DesignExamServiceImpl extends GenericServiceImpl<DesignExam, String> implements
		DesignExamService {

	@Autowired
	private DesignExamDao designExamDao;
	
	@Autowired
	private DesignExamAttDao designExamAttDao;
	
	@Autowired
	private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<DesignExam, String> getDao() {
		return designExamDao;
	}

    @Override
    public Page<DesignExamDomain> selectByDomainWhere(Page<DesignExamDomain> page,
                                                      DesignExamVo designExamVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DesignExamDomain>)designExamDao.selectByDomainWhere(designExamVo);
    }

    @Override
    public Page<DesignExamDomain> selectMyCheckByDomainWhere(Page<DesignExamDomain> page,
                                                             DesignExamVo designExamVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DesignExamDomain>)designExamDao.selectMyCheckByDomainWhere(designExamVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDesignExam(DesignExam designExam, List<DesignExamAtt> designExamAtts) {
        
        designExamDao.insertById(designExam);
        
        if(CollectionUtils.isNotEmpty(designExamAtts)){
            designExamAttDao.insertByBatch(designExamAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.IC_DESIGN_EXAM;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, designExam.getId(), designExam.getProcessId(), null);
        
    }

    @Override
    public DesignExamVo queryById(String id) {
        return designExamDao.queryById(id);
    }

}